<?php

namespace frontend\models;

use common\models\user\UserFollower;
use Yii;
use yii\filters\RateLimitInterface;
use yii\helpers\ArrayHelper;

/**
 * 兼容模式下的用户信息模型
 * UserForLogin
 */
class UserCompatibleModel extends User {

    public function fields() {
        //兼容原接口字段
        $old = [
            'groupid',
            'memberid',
            'ships',
            'medals',
            'birthday',
            'register_at' => 'registerAt',
            'last_login' => 'lastLogin',
            'group',
            'is_authent' => 'isAuthent',
            'authent',
            'feeling',
            'followers',
            'fans',
            'level',
            'gender',
            'has_card' => 'hasCard',
            'following',
            "fans",
            "level",
            "gender",
            "following",
            "remark",
            "fan",
            "is_live" => 'isLive',
            "host",
            "threads",
            "posts",
            "is_staff" => 'isStaff',
            "vip",
            "is_vip" => 'isVip',
            "detail",
            "heat",
            "giving",
            "room",
            "credit",
            "rongcloud",
            "address",
            "checked_in" => 'checkedIn',
            "card",
            "association",
            "annual",
            "server_time" => 'serverTime'
        ];
        
        return ArrayHelper::merge(parent::fields(), $old);
    }

    /**
     * 兼容原登录接口，匹配内容
     */
    public function getGroupid() {
        return 0;
    }

    public function getMemberid() {
        return $this->id;
    }

    public function getShips() {
        return '';
    }

    public function getMedals() {
        return null;
    }

    public function getPortrait() {
        return $this->avatar;
    }

    public function getBirthday() {
        if ($user = $this->userAuthent) {
            return $user->birthday;
        }
        return '';
    }

    public function getRegisterAt() {
        return date('c', $this->created_at);
    }

    public function getLastLogin() {
        if ($this->token) {
            return date('c', $this->token->updated_at);
        }
        return null;
    }

    public function getGroup() {
        return null;
    }

    public function getIsAuthent() {
        return false;
    }

    public function getAuthent() {
        return null;
    }

    /**
     * 签名
     * @return string
     */
    public function getFeeling() {
        return '';
    }

    public function getFollowers() {
        return $this->getUserFollower()->count('id');
    }

    /**
     * 粉丝数
     * @return int
     */
    public function getFans() {
        return UserFollower::find()->where(['followed_user_id'=>Yii::$app->user->id])->count('id');
    }

    /**
     * 级别
     * @return int
     */
    public function getLevel() {
        return 0;
    }

    public function getGender() {
        if ($authent = $this->userAuthent) {
            return $authent->gender;
        }
        return 0;
    }

    /**
     * 是否拥有会员卡
     * @return int
     */
    public function getHasCard() {
        return false;
    }

    /**
     * 是否被关注
     * @return bool
     */
    public function getFollowing() {
        if ($this->userFollowerForFaned) {
            return true;
        }
        return false;
    }

    /**
     * 关注人备注名
     * @return null
     */
    public function getRemark() {
        if ($fan = $this->isUserFollowerForFanded) {
            return $fan->remark;
        }
        return null;
    }

    public function getFan() {
        if ($this->userFollowerForFaned) {
            return true;
        }
        return null;
    }

    /**
     * 是否是主播用户
     * ！暂时无表
     * @return bool
     */
    public function getIsLive() {
        return false;
    }

    public function getHost() {
        return true;
    }

    /**
     * 圈子数
     */
    public function getThreads() {
        return 0;
    }

    /**
     * 发帖数
     * @return int
     */
    public function getPosts() {
        return 0;
    }

    public function getIsStaff() {
        return false;
    }

    public function getVip() {
        return null;
    }

    /**
     * 是否是会员
     * @return null
     */
    public function getIsVip() {
        return false;
    }

    public function getDetail() {
        if ($authent = $this->userAuthent) {
            return [
                'gender' => $authent->gender,
                'birthday' => $authent->birthday,
                'feeling' => ""
            ];
        }
        return ['gender' => 0, 'birthday' => null, 'feeling' => ""];
    }

    /**
     * 接收礼物数
     */
    public function getHeat() {
        return 0;
    }

    /**
     * 赠送礼物数
     * @return int
     */
    public function getGiving() {
        return 0;
    }

    /**
     * 所属直播间
     * @return null
     */
    public function getRoom() {
        return null;
    }

    public function getCredit() {
        return [
            'gold' => 0,
            'orange' => 0,
            'exps' => 0,
            'cash' => 0,
        ];
    }

    public function getRongCloud() {
        return null;
    }

    public function getAddress() {
        return null;
    }

    public function getCheckedIn() {
        return false;
    }

    public function getCard() {
        return null;
    }

    public function getAssociation() {
        return null;
    }

    public function getAnnual() {
        return null;
    }

    public function getServerTime() {
        return date('c');
    }

}
